home *** CD-ROM | disk | FTP | other *** search
- #ifndef lint
- static char *RCSid = "$Header: ntpsubs.c,v 2.14 89/02/19 16:08:52 bww Exp $";
- #endif
-
- /*
- ****************************************************************
- * HISTORY
- * $Log: ntpsubs.c,v $
- * Revision 2.14 89/02/19 16:08:52 bww
- * SSP headers.
- * [89/02/19 16:07:17 bww]
- *
- * Revision 2.13 88/11/17 22:30:41 bww
- * Allow for negative packet delays.
- *
- * Revision 2.12 88/11/11 16:07:15 bww
- * Upgraded to latest UMD release.
- *
- * Revision 2.11 88/10/03 23:30:39 bww
- * Revamped getinaddr() to return useful information
- * for multi-homed hosts (at least around here).
- *
- * Revision 2.10 88/09/20 21:59:48 bww
- * Fixed double to u_long conversions.
- *
- * Revision 2.9 88/08/25 14:08:51 bww
- * More rationalizations.
- *
- * Revision 2.8 88/08/22 16:48:57 bww
- * Rationalized type conversion fixes.
- *
- * Revision 2.7 88/08/22 00:59:52 bww
- * Fixed falsetick() to only sort once.
- *
- * Revision 2.6 88/08/20 19:04:10 bww
- * Fixed some "cannot happens" that were happening.
- *
- * Revision 2.5 88/08/20 17:00:04 bww
- * Moved getinaddr() here.
- *
- * Revision 2.4 88/08/18 21:29:01 bww
- * Fixed handling of signed octets to be machine independent.
- *
- * Revision 2.3 88/07/17 15:07:01 bww
- * First CMUCS release
- *
- * Revision 2.2 88/05/13 10:47:34 root
- * Initial CMU revision
- *
- * Revision 2.2 88/05/13 10:47:34 root
- * *** empty log message ***
- *
- * Revision 2.1 88/04/25 18:00:26 root
- * *** empty log message ***
- *
- * Revision 2.0 88/03/31 10:25:27 root
- * *** empty log message ***
- *
- * Revision 1.2 88/02/28 22:59:49 petry
- * *** empty log message ***
- *
- * Revision 1.1 87/12/16 10:33:21 root
- * Initial revision
- *
- */
-
- #include <Types.h>
- #include <Math.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <ErrMgr.h>
- #include <CursorCtl.h>
- #include <Errors.h>
- #include <Devices.h>
- #include <MacTCPCommonTypes.h>
- #include <UDPPB.h>
- #include <TCPPB.h>
- #include <GetMyIPAddr.h>
- #include <AddressXlation.h>
- #include <Time.h>
- #include <Script.h>
- #define fraction fraction /* the script manager does this */
-
- #include "ntp.h"
-
-
- #ifdef DEBUG
- extern int debug;
- #endif
-
- #define BIT7 ((u_int) (1<<7))
- #define BIT15 ((u_int) (1<<15))
- #define BIT31 ((u_long) (1<<31))
-
- #define TWO16 ((double) 65536.0)
- #define TWO31 ((double) 2.147483648e9)
- #define TWO32 ((double) 4.294967296e9)
-
- #define signed8(b) ((b) & BIT7)
- #define signed16(b) ((b) & BIT15)
- #define signed32(b) ((b) & BIT31)
-
- #define lshift16(a) ((a) *= TWO16)
- #define lshift32(a) ((a) *= TWO32)
-
- #define rshift16(a) ((a) /= TWO16)
- #define rshift32(a) ((a) /= TWO32)
-
- /*
- * some compilers seem to have trouble with these conversions
- * when the top bit is on, so let's spell it out
- */
- #define double_to_u_long(a) ((TWO31<=(a)&&(a)<TWO32)?\
- ((u_long)((a)-TWO31))|BIT31:(u_long)(a))
- #define u_long_to_double(b) (signed32(b)?((b)^BIT31)+TWO31:(double)(b))
-
- double
- ul_fixed_to_double(t)
- struct l_fixedpt *t;
- {
- double a;
- u_long b;
-
- b = ntohl(t->fraction);
- a = u_long_to_double(b);
- rshift32(a);
- b = ntohl(t->int_part);
- a += u_long_to_double(b);
- return (a);
- }
-
- double
- l_fixed_to_double(t)
- struct l_fixedpt *t;
- {
- double a;
- u_long b;
-
- if (signed32(ntohl(t->int_part))) {
- b = ntohl(~t->fraction);
- a = u_long_to_double(b);
- rshift32(a);
- a += ntohl(~t->int_part);
- a = -a;
- } else {
- b = ntohl(t->fraction);
- a = u_long_to_double(b);
- rshift32(a);
- a += ntohl(t->int_part);
- }
- return (a);
- }
-
- double
- s_fixed_to_double(t)
- struct s_fixedpt *t;
- {
- double a;
-
- if (signed16(ntohs(t->sint_part))) {
- a = ntohs(~t->sfraction & 0xffff);
- rshift16(a);
- a += ntohs(~t->sint_part & 0xffff);
- a = -a;
- } else {
- a = ntohs(t->sfraction);
- rshift16(a);
- a += ntohs(t->sint_part);
- }
- return (a);
- }
-
- double_to_l_fixed(t, value)
- struct l_fixedpt *t;
- double value;
- {
- double temp;
-
- if (value < 0.0) {
- value = -value;
- t->int_part = double_to_u_long(value);
- temp = value - t->int_part;
- lshift32(temp);
- t->fraction = double_to_u_long(temp);
- t->int_part = htonl(~t->int_part);
- t->fraction = htonl(~t->fraction);
- } else {
- t->int_part = double_to_u_long(value);
- temp = value - t->int_part;
- lshift32(temp);
- t->fraction = double_to_u_long(temp);
- t->int_part = htonl(t->int_part);
- t->fraction = htonl(t->fraction);
- }
- }
-
- double_to_s_fixed(t, value)
- struct s_fixedpt *t;
- double value;
- {
- double temp;
-
- if (value < 0.0) {
- value = -value;
- t->sint_part = value;
- temp = value - t->sint_part;
- lshift16(temp);
- t->sfraction = temp;
- t->sint_part = htons(~t->sint_part);
- t->sfraction = htons(~t->sfraction);
- } else {
- t->sint_part = value;
- temp = value - t->sint_part;
- lshift16(temp);
- t->sfraction = temp;
- t->sint_part = htons(t->sint_part);
- t->sfraction = htons(t->sfraction);
- }
- }
-
- int
- char_to_int(c)
- int c;
- {
- if (signed8(c))
- return ((~0<<8) | c); /* sign extend */
- return (c);
- }
-
- int_to_char(p, i)
- char *p;
- int i;
- {
- *p = i;
- }
-
- /* BEGIN MAC SPECIFIC FUNCTIONS */
- /* gettimeofday is a unix function that is supposed to return the time of day
- * in seconds since Jan 1, 1970. Mac time is returned in seconds since
- * Jan 1, 1900.
- * We do the conversion of seconds here.
- */
-
- #define YEARDIFF (66)
- #define SECSperMINUTE (60)
-
- static long gmtDelta = 0;
-
- gettimeofday(tp,tzp)
- struct timeval *tp;
- struct timezone *tzp;
- {
- #pragma unused(tzp)
-
- time_t thetime;
- struct tm *thetm;
-
-
- time(&thetime);
- thetm = localtime(&thetime);
- //fprintf(stderr,"gettimeofday: given %s\n",asctime(thetm));
-
- /* subtract YEARDIFF years */
- thetm->tm_year -= YEARDIFF;
- /* convert back to seconds */
- thetime = mktime(thetm);
-
- /* convert to GMT */
- thetime -= (gmtDelta);
-
- tp->tv_sec = thetime;
- tp->tv_usec = 0; /* on the mac,always */
-
- //fprintf(stderr,"gettimeofday: %s\n",asctime(localtime(&tp->tv_sec)));
- }
-
- static time_t unixtomacsecs(const time_t *unixsecs)
- {
- time_t thetime;
- struct tm *thetm;
-
- thetime = *unixsecs;
-
- thetm = localtime(&thetime);
-
- //fprintf(stderr,"unixtomacsecs: given %s\n",asctime(thetm));
-
- /* add YEARDIFF years */
- thetm->tm_year += YEARDIFF;
- /* convert back to seconds */
- thetime = mktime(thetm);
-
- /* convert to GMT */
- thetime += (gmtDelta);
-
- //fprintf(stderr,"unixtomacsecs: %s\n",asctime(localtime(&thetime)));
-
- return thetime;
- }
-
- settimeofday(tp,tzp)
- struct timeval *tp;
- struct timezone *tzp;
- {
- #pragma unused(tzp)
- time_t thetime;
-
- thetime = unixtomacsecs(&(tp->tv_sec));
-
- /* set the clock */
-
- if (SetDateTime(thetime) != noErr) {
- return (-1);
- }
- }
-
- void setminuteseast(long num)
- {
- MachineLocation loc;
- long internalgmtdelta;
-
-
- if (num) {
- gmtDelta = num * SECSperMINUTE;
- } else {
- /* get from the PRAM */
-
- ReadLocation(&loc);
- internalgmtdelta = loc.gmtFlags.gmtDelta & 0x00ffffff;
-
- if ( (internalgmtdelta >> 23) & 1) { /* need to sign extend */
- internalgmtdelta = internalgmtdelta | 0xff000000;
- }
-
- gmtDelta = internalgmtdelta;
- if (loc.gmtFlags.dlsDelta != 0) {
- fprintf(stderr,"Warning: not using Daylight Savings flag (value = %u)\n",loc.gmtFlags.dlsDelta);
- }
- }
- }
-
- char *
- macctime(time_t *unixsecs)
- {
- time_t thetime;
-
- thetime = unixtomacsecs(unixsecs);
-
- return asctime(localtime(&thetime));
- }
-
-
- /* END MAC SPECIFIC FUNCTIONS */
-
-
- tstamp(stampp, tvp)
- struct l_fixedpt *stampp;
- struct timeval *tvp;
- {
- double temp;
-
- stampp->int_part = JAN_1970 + tvp->tv_sec;
- temp = u_long_to_double(tvp->tv_usec) / 1e6;
- lshift32(temp);
- stampp->fraction = double_to_u_long(temp);
- stampp->int_part = htonl(stampp->int_part);
- stampp->fraction = htonl(stampp->fraction);
- }
-
- double
- minimum_filter(delay, offset, count)
- register double *delay;
- register double *offset;
- register int count;
- {
- register int i, j;
- double temp, dsp;
-
- for (i = 0; i < count - 1; i++) {
- dsp = fabs(delay[i]);
- for (j = i + 1; j < count; j++) {
- if (dsp > fabs(delay[j])) {
- temp = delay[i];
- delay[i] = delay[j];
- delay[j] = temp;
- dsp = fabs(delay[i]);
- temp = offset[i];
- offset[i] = offset[j];
- offset[j] = temp;
- }
- }
- }
- /* we are now sorted by |delay| */
- dsp = 0.0;
- for (i = count - 1; i > 0; i--) {
- temp = (fabs(delay[i]) >= NTP_MAXDELAY) ?
- NTP_MAXDISP : (offset[i] - offset[0]);
- if (temp < 0.0)
- temp = -temp;
- if (temp > NTP_MAXDISP)
- temp = NTP_MAXDISP;
- dsp /= 2.0;
- dsp += temp;
- }
- dsp /= 2.0;
- return (dsp);
- }
-
- falsetick(sl, count)
- register struct select_list *sl;
- register int count;
- {
- register int i, j, maxind;
- struct select_list temp;
- double diff, dsp, off;
-
- for (i = 0; i < count - 1; i++) {
- for (j = i + 1; j < count; j++) {
- if (sl[i].sort_item > sl[j].sort_item) {
- temp = sl[i];
- sl[i] = sl[j];
- sl[j] = temp;
- }
- }
- }
- /* we are now sorted by sort_item */
- do {
- for (i = 0; i < count; i++) {
- dsp = 0.0;
- off = sl[i].peer->offset;
- for (j = count - 1; j >= 0; j--) {
- diff = sl[j].peer->offset - off;
- if (diff < 0.0)
- diff = -diff;
- if (diff > NTP_MAXDISP)
- diff = NTP_MAXDISP;
- dsp = (dsp * PEER_SELECT) + diff;
- }
- sl[i].dsp = dsp * PEER_SELECT;
- #ifdef DEBUG
- if (debug > 1)
- printf("sl[%d] = %s %x %f\n",
- i,
- /* inet_ntoa((sl[i].peer)->src.sin_addr) */ "??",
- sl[i].sort_item,
- sl[i].dsp);
- #endif
- }
- /*
- * Remove entry with the largest dispersion
- */
- for (maxind = 0, i = 1; i < count; i++)
- if (sl[i].dsp >= sl[maxind].dsp)
- maxind = i;
- for (i = maxind + 1; i < count; i++)
- sl[i - 1] = sl[i];
- } while (--count > 0);
- }
-